package com.designoutput.manager.fontManagerClasses
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import ru.etcs.utils.FontLoader;

	public class FontStyleInfo
	{
		private const FONTS_PATH:String = "config/fonts/";
		
		public var fontName:String;
		public var bold:Boolean = false;
		public var italic:Boolean = false;
		public var fileName:String;
		public var fontType:String;
		
		public var fontBytes:ByteArray;
		
		private var _loading:Boolean = false;
		private var _loaded:Boolean = false;
		private var _loadedSWF:Boolean = false;
		private var _loadedBytes:Boolean = false;
		private var _fontLoader:FontLoader;
		private var _fontBytesLoader:URLLoader;
		private var _onReadyFunctions:Vector.<Function>;
		
		public static function parseFromXML(data:XML):FontStyleInfo
		{
			var obj:FontStyleInfo = new FontStyleInfo();
			obj.bold = String(data.@bold) == "true";
			obj.italic = String(data.@italic) == "true";
			obj.fileName = String(data.@fileName);
			obj.fontType = String(data.@fontType);
			return obj;
		}
		
		public function require(onReady:Function):void {
			if(_loaded) {
				onReady();
			} else if(_loading) {
				_onReadyFunctions.push(onReady);
			} else {
				_loading = true;
				
				_onReadyFunctions = new Vector.<Function>();
				_onReadyFunctions.push(onReady);
				
				_fontBytesLoader = new URLLoader();
				_fontBytesLoader.dataFormat = URLLoaderDataFormat.BINARY;
				_fontBytesLoader.addEventListener(Event.COMPLETE, _fontBytesLoader_completeHandler, false, 0, true);
				_fontBytesLoader.addEventListener(IOErrorEvent.IO_ERROR, _fontBytesLoader_failHandler, false, 0, true);
				
				//_fontBytesLoader.load(new URLRequest(/*Globals.applicationUrl + */FONTS_PATH + fileName + "." + fontType));
				
				_fontLoader = new FontLoader();
				_fontLoader.addEventListener(Event.COMPLETE, _fontLoader_completeHandler, false, 0, true);
				_fontLoader.addEventListener(IOErrorEvent.IO_ERROR, _fontLoader_failHandler, false, 0, true);
				_fontLoader.addEventListener(IOErrorEvent.VERIFY_ERROR, _fontLoader_failHandler, false, 0, true);
				_fontLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _fontLoader_failHandler, false, 0, true);
				
				_fontLoader.load(new URLRequest(/*Globals.applicationUrl + */FONTS_PATH + fileName + ".swf"), true);
			}
		}
		
		private function _fontLoader_completeHandler(e:Event):void {
			_fontLoader = null;
			
			_loadedSWF = true;
			check();
		}
		
		private function _fontLoader_failHandler(e:Event):void {
			_fontLoader = null;
		}
		
		private function _fontBytesLoader_completeHandler(e:Event):void {
			fontBytes = _fontBytesLoader.data;
			_fontBytesLoader = null;
			
			_loadedBytes = true;
			check();
		}
		
		private function _fontBytesLoader_failHandler(e:Event):void {
			_fontBytesLoader = null;
		}
		
		private function check():void {
			/*if(!_loadedSWF || !_loadedBytes)
				return;*/
			
			for each(var fn:Function in _onReadyFunctions)
				fn();
			
			_onReadyFunctions = null;
			
			_loading = false;
			_loaded = true;
		}
	}
}